package com.liting.uitest.imgutils;

import com.liting.uitest.model.Circle;
import com.liting.uitest.model.ImageInfo;
import com.liting.uitest.model.Rectangle;
import com.liting.uitest.service.impl.ImageCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 留白率计算
 */
@Component
@Slf4j
public class WhiteSpace {
    @Autowired
    private ImageCacheService cacheService;

    public BigDecimal whiteSpaceCalc(Long fileId, List<Rectangle> rectList, List<Circle> cirList) throws Exception {
        ImageInfo imageInfo = cacheService.getImage(fileId);
        if (imageInfo == null) {
            throw new Exception("文件不存在");
        }

        BigDecimal area = new BigDecimal(imageInfo.getHeight()).multiply(new BigDecimal(imageInfo.getWidth())).setScale(2, RoundingMode.UP);
        BigDecimal area1 = new BigDecimal(0);
        for (Rectangle rect : rectList) {
            area1 = area1.add(rect.getArea());
        }
        for (Circle circle : cirList) {
            area1 = area1.add(circle.getArea());
        }

        BigDecimal ws = new BigDecimal(1).subtract(area1.divide(area, RoundingMode.UP));

        return ws.setScale(1, RoundingMode.UP);

    }
}
